package com.start.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.admin.entity.StartSysMenu;
import com.start.admin.entity.StartSysRole;
import com.start.admin.service.StartSysRoleMenuService;
import com.start.admin.service.StartSysRoleService;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.details.BaseUserDetails;
import com.start.common.entity.StartBaseMenu;
import com.start.common.entity.StartBaseRole;
import com.start.common.utils.TreeModelUtils;
import com.start.core.annotations.StartLog;
import com.start.core.vo.RoleMenuVo;
import com.start.security.StartTokenTools;
import com.start.security.StartUserPermissionTools;
import com.start.security.annotations.DataPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/start/sysRole")
public class StartSysRoleController {

    @Autowired
    private StartSysRoleService startSysRoleService;


    @Autowired
    private StartUserPermissionTools userPermissionTools;
    @Autowired
    private StartSysRoleMenuService sysRoleMenuService;

    @GetMapping("/page")
    @DataPermission("views/sys/role/index")
    public Result<?> page(Page page, StartSysRole role) {
        long time = System.currentTimeMillis();
        QueryWrapper<StartSysRole> sysUserQueryWrapper = DataQueryConstructor.createEntity(StartSysRole.class);
        List<StartSysRole> list = startSysRoleService.list(sysUserQueryWrapper);
        List<StartSysRole> tree = TreeModelUtils.createTree(list);
        System.out.println("耗时：" + (System.currentTimeMillis() - time));
        return Result.ok(tree);
    }

    @PostMapping("/add")
    @PreAuthorize("@pas.hasPermission('sys:role:add')")
    @StartLog(value = "系统角色管理", action = "添加角色")
    public Result<?> add(@RequestBody StartSysRole role) {
        startSysRoleService.createRole(role);
        return Result.ok();
    }

    @PutMapping("/updateById")
    @PreAuthorize("@pas.hasPermission('sys:role:edit')")
    public Result updateById(@RequestBody StartSysRole role) {
        startSysRoleService.updateRoleById(role);
        return Result.ok();
    }

    @GetMapping("/getById/{id}")
    @PreAuthorize("@pas.hasPermission('sys:role:info')")
    public Result getById(@PathVariable("id") String id) {
        return Result.ok(startSysRoleService.getById(id));
    }

    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('sys:role:delete')")
    public Result deleteById(@PathVariable("id") String id) {
        startSysRoleService.deleteRole(id);
        return Result.ok();
    }

    @GetMapping("/getRoleMenu/{roleId}")
    public Result getRoleMenu(@PathVariable("roleId") String roleId) {
        List<StartSysMenu> list = startSysRoleService.listRoleMenuByRoleId(roleId);
        return Result.ok(list);
    }

    @GetMapping("/getCurrentUserMenu")
    public Result getCurrentUserMenu() {
        BaseUserDetails currentUser = StartTokenTools.getCurrentUser();
        List<StartBaseMenu> userMenu = userPermissionTools.getUserMenu(currentUser);
        return Result.ok(userMenu);
    }

    @PostMapping("/saveRoleMenu")
    public Result saveRoleMenu(@RequestBody RoleMenuVo roleMenuVo) {
        sysRoleMenuService.save(roleMenuVo);
        return Result.ok();
    }

    @GetMapping("/listTree")
    public Result<?> listTree() {
        BaseUserDetails currentUser = StartTokenTools.getCurrentUser();
        List<StartBaseRole> userRole = userPermissionTools.getUserRole(currentUser);
        List<StartBaseRole> tree = TreeModelUtils.createTree(userRole);
        return Result.ok(tree);
    }
}
